package de.blitzcoder.cddatabase.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author blitzcoder
 */
public class CD implements Serializable {

    private static int idCount;

    private String  name;
    private int     year;
    private int     id;
    private String  artist;

    private ArrayList<Track> tracks;

    static {
        idCount = 0;
    }

    public CD() {
        this("",new GregorianCalendar().get(GregorianCalendar.YEAR),"");
    }

    public CD(String name, int year, String artist) {
        this.id = idCount;
        idCount++;

        this.name = name;
        this.year = year;
        this.artist = artist;
        this.tracks = new ArrayList<Track>();
    }

    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof CD))
            return false;
        CD cd = (CD) o;
        return name.equals(cd.getName()) && year == cd.getYear() && artist.equals(cd.getArtist()) && tracks.equals(cd.getTrackList());
    }

    public void setYear(int year) {
        this.year = year;
    }

    public List<Track> getTrackList() {
        return tracks;
    }
    
    public void setArtist(String artist) {
        this.artist = artist;
    }

    public int getYear() {
        return year;
    }

    public int getID() {
        return id;
    }

    public void setTracks(Track[] tracks) {
        this.tracks = new ArrayList<Track>(Arrays.asList(tracks));
    }
    
    public Track[] getTracks() {
        return tracks.toArray(new Track[0]);
    }

    public int getDuration() {
        int sec = 0;
        ListIterator<Track> it = tracks.listIterator();
        while (it.hasNext()) {
            sec+=it.next().getDuration();
        }
        return sec;
    }

    public String getArtist() {
        return artist;
    }

    public String getName() {
        return name;
    }
    

}
